<?php

namespace App\Models\Home;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class Notice extends Model
{
    protected $table = 'notice';
    protected $guarded = [];

    public function getList($between=array(),$skip=0,$limit,$opt=array(),$push_time=false){
        $query = self::orderBy('id','desc');

        if ($between && count($between) == 2){
            $query = $query->whereBetween('updated_at', $between);
        }

        if ($push_time){
            $query = $query->where('start_time', '<=', Carbon::now());
            $query = $query->where('end_time', '>', Carbon::now());
        }

        if (!empty($opt)){
            foreach ($opt as $k => $v){
                if(is_array($v) && $v[1]){
                    $query = $query->where($k,$v[0],$v[1]);
                }elseif(!is_array($v) && $v !== false){
                    $query = $query->where($k,$v);
                }
            }
        }

        if (isset($limit)){
            $query = $query->skip($skip)->limit($limit);
        }

        $list = $query->get();

        return $list;

    }

    public function getCount($between=array(), $opt=array()){
        $query = self::orderBy('id','desc');

        if (!empty($between)){
            $query = $query->whereBetween('updated_at', $between);
        }

        if (!empty($opt)){
            foreach ($opt as $k => $v){
                if(is_array($v) && $v[1]){
                    $query = $query->where($k,$v[0],$v[1]);
                }elseif(!is_array($v) && $v !== false){
                    $query = $query->where($k,$v);
                }
            }
        }

        $count = $query->count();

        return $count;
    }

}
